package com.ccsoft.cc.bottomscrollmenu;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

/**
 * TODO: document your custom view class.
 */
public class ScrollMenu extends ScrollView {

    private LinearLayout rootLayout;
    private View contentView;
    private View menuLayout;
    private int t;
    private boolean isVisiable, moveAble;

    public ScrollMenu(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public ScrollMenu(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ScrollMenu(Context context) {
        super(context);
        init(context, null);
    }

    private void init(Context context, AttributeSet attrs) {
        rootLayout = new LinearLayout(context);
        rootLayout.setOrientation(LinearLayout.VERTICAL);
        contentView = LayoutInflater.from(context).inflate(R.layout.layout_content, null);
        menuLayout = LayoutInflater.from(context).inflate(R.layout.layout_munu, null);
        rootLayout.addView(contentView);
        rootLayout.addView(menuLayout);
        addView(rootLayout);
        findViewById(R.id.button2).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                smoothScrollTo(0, menuLayout.getHeight());
            }
        });
        setVerticalScrollBarEnabled(false);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        this.t = t;
        Log.i("CC", "t:" + t + ",oldt:" + oldt);//向下滑动时，t是减小的。
        float pocent = t / (float) menuLayout.getHeight();
        contentView.setTranslationY(t);
//        contentView.setScaleY(1 - pocent);
//        contentView.setScaleX(1 - pocent);
    }

    private boolean once = true;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);

        //因为onMeasure会多次执行。第一次的时候先测量父控件的尺寸。
        if (once)
            contentView.setLayoutParams(new LinearLayout.LayoutParams(width, height));//把contentView的大小充满屏幕。
        once = false;

    }

    private float touchY;
    private float mLoastTouchY;
    private float dis;
    private float offset = 50;

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        {
            switch (ev.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touchY = t;
                    moveAble = false;
                    mLoastTouchY = ev.getY();
                    return true;
                case MotionEvent.ACTION_MOVE:
                    dis = mLoastTouchY - ev.getY();
//                    Log.i("CC", "ACTION_MOVE" + ev.getY() + ",mDis:" + dis);
                    ((TextView) findViewById(R.id.what)).setText("ACTION_MOVE" + ev.getY() + ",mDis:" + dis);
                    //判断滑动的距离，如果大于offset的话就可以开始滑动了。
                    if (dis >= offset)
                        moveAble = true;

                    //如果菜单是显示状态，不判断它的滑动距离，直接可以滑动。
                    if (isVisiable) {
                        return super.onTouchEvent(ev);
                    } else {
                        if (moveAble)
                            return super.onTouchEvent(ev);
                        else
                            return true;
                    }
                case MotionEvent.ACTION_UP:
                    float mDis = touchY - t;//判断它滑动的方向，大于0是向下，小于0是向上。

                    if (dis < 0 && !isVisiable)//已经是不可见状态，再向下滑动没有效果。
                        return true;

                    if (Math.abs(dis) < offset) {
                        if (isVisiable)
                            showMenu();
                        else
                            hideMenu();
                    } else if (mDis > 0) {
                        hideMenu();
                    } else {
                        showMenu();
                    }

                    dis = 0;
                    return true;
            }
            return super.onTouchEvent(ev);
        }

    }

    public void showMenu() {
        smoothScrollTo(0, menuLayout.getHeight());
        isVisiable = true;
    }

    public void hideMenu() {
        smoothScrollTo(0, 0);
        isVisiable = false;
    }

    //    private String mExampleString; // TODO: use a default from R.string...
//    private int mExampleColor = Color.RED; // TODO: use a default from R.color...
//    private float mExampleDimension = 0; // TODO: use a default from R.dimen...
//    private Drawable mExampleDrawable;
//
//    private TextPaint mTextPaint;
//    private float mTextWidth;
//    private float mTextHeight;
//
//    public ScrollMenu(Context context) {
//        super(context);
//        init(null, 0);
//    }
//
//    public ScrollMenu(Context context, AttributeSet attrs) {
//        super(context, attrs);
//        init(attrs, 0);
//    }
//
//    public ScrollMenu(Context context, AttributeSet attrs, int defStyle) {
//        super(context, attrs, defStyle);
//        init(attrs, defStyle);
//    }
//
//    private void init(AttributeSet attrs, int defStyle) {
//        // Load attributes
//        final TypedArray a = getContext().obtainStyledAttributes(
//                attrs, R.styleable.ScrollMenu, defStyle, 0);
//
//        mExampleString = a.getString(
//                R.styleable.ScrollMenu_exampleString);
//        mExampleColor = a.getColor(
//                R.styleable.ScrollMenu_exampleColor,
//                mExampleColor);
//        // Use getDimensionPixelSize or getDimensionPixelOffset when dealing with
//        // values that should fall on pixel boundaries.
//        mExampleDimension = a.getDimension(
//                R.styleable.ScrollMenu_exampleDimension,
//                mExampleDimension);
//
//        if (a.hasValue(R.styleable.ScrollMenu_exampleDrawable)) {
//            mExampleDrawable = a.getDrawable(
//                    R.styleable.ScrollMenu_exampleDrawable);
//            mExampleDrawable.setCallback(this);
//        }
//
//        a.recycle();
//
//        // Set up a default TextPaint object
//        mTextPaint = new TextPaint();
//        mTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
//        mTextPaint.setTextAlign(Paint.Align.LEFT);
//
//        // Update TextPaint and text measurements from attributes
//        invalidateTextPaintAndMeasurements();
//    }
//
//    private void invalidateTextPaintAndMeasurements() {
//        mTextPaint.setTextSize(mExampleDimension);
//        mTextPaint.setColor(mExampleColor);
//        mTextWidth = mTextPaint.measureText(mExampleString);
//
//        Paint.FontMetrics fontMetrics = mTextPaint.getFontMetrics();
//        mTextHeight = fontMetrics.bottom;
//    }
//
//    @Override
//    protected void onDraw(Canvas canvas) {
//        super.onDraw(canvas);
//
//        // TODO: consider storing these as member variables to reduce
//        // allocations per draw cycle.
//        int paddingLeft = getPaddingLeft();
//        int paddingTop = getPaddingTop();
//        int paddingRight = getPaddingRight();
//        int paddingBottom = getPaddingBottom();
//
//        int contentWidth = getWidth() - paddingLeft - paddingRight;
//        int contentHeight = getHeight() - paddingTop - paddingBottom;
//
//        // Draw the text.
//        canvas.drawText(mExampleString,
//                paddingLeft + (contentWidth - mTextWidth) / 2,
//                paddingTop + (contentHeight + mTextHeight) / 2,
//                mTextPaint);
//
//        // Draw the example drawable on top of the text.
//        if (mExampleDrawable != null) {
//            mExampleDrawable.setBounds(paddingLeft, paddingTop,
//                    paddingLeft + contentWidth, paddingTop + contentHeight);
//            mExampleDrawable.draw(canvas);
//        }
//    }
//
//    /**
//     * Gets the example string attribute value.
//     *
//     * @return The example string attribute value.
//     */
//    public String getExampleString() {
//        return mExampleString;
//    }
//
//    /**
//     * Sets the view's example string attribute value. In the example view, this string
//     * is the text to draw.
//     *
//     * @param exampleString The example string attribute value to use.
//     */
//    public void setExampleString(String exampleString) {
//        mExampleString = exampleString;
//        invalidateTextPaintAndMeasurements();
//    }
//
//    /**
//     * Gets the example color attribute value.
//     *
//     * @return The example color attribute value.
//     */
//    public int getExampleColor() {
//        return mExampleColor;
//    }
//
//    /**
//     * Sets the view's example color attribute value. In the example view, this color
//     * is the font color.
//     *
//     * @param exampleColor The example color attribute value to use.
//     */
//    public void setExampleColor(int exampleColor) {
//        mExampleColor = exampleColor;
//        invalidateTextPaintAndMeasurements();
//    }
//
//    /**
//     * Gets the example dimension attribute value.
//     *
//     * @return The example dimension attribute value.
//     */
//    public float getExampleDimension() {
//        return mExampleDimension;
//    }
//
//    /**
//     * Sets the view's example dimension attribute value. In the example view, this dimension
//     * is the font size.
//     *
//     * @param exampleDimension The example dimension attribute value to use.
//     */
//    public void setExampleDimension(float exampleDimension) {
//        mExampleDimension = exampleDimension;
//        invalidateTextPaintAndMeasurements();
//    }
//
//    /**
//     * Gets the example drawable attribute value.
//     *
//     * @return The example drawable attribute value.
//     */
//    public Drawable getExampleDrawable() {
//        return mExampleDrawable;
//    }
//
//    /**
//     * Sets the view's example drawable attribute value. In the example view, this drawable is
//     * drawn above the text.
//     *
//     * @param exampleDrawable The example drawable attribute value to use.
//     */
//    public void setExampleDrawable(Drawable exampleDrawable) {
//        mExampleDrawable = exampleDrawable;
//    }

    protected DisplayMetrics getScreen() {
        return getResources().getDisplayMetrics();
    }
}
